using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
    public float speed = 1.0f;
    public float liftTime = 3.0f;
    GameObject target;
    public float atk = 10;

    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject,liftTime);
    }

    // Update is called once per frame
    private void FixedUpdate()
    {
        transform.position += transform.right * speed * Time.fixedDeltaTime;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Wall"))
        {
            Destroy(gameObject);
            print(collision.gameObject.name);
        }

        if (collision.gameObject.CompareTag("Enemy"))
        {
            Destroy(gameObject);
            print(collision.gameObject.name);
            target = collision.gameObject;
            TargetBeAttack();
        }
        //TargetBeAttack();
    }

    public IAttack GetAttackTarget()
    {
        if(target != null)
        {
            return target.GetComponent<IAttack>();
        }
        else
        {
            return null;
        }
    }

    public void TargetBeAttack()
    {
        var target = GetAttackTarget();
        target?.BeAttacked(gameObject);
    }
}
